package requerst;

import java.io.*;
import java.net.*;
import java.util.Map;
import java.util.Set;

public class HttpUtils {
/**
 * @author: dae
 * @email: 675116529@qq.com
 * @phone: 15270029507
 * @DateTime: 2021/12/13 16:39
 *get,post封装
 * @return*/



//该函数用于实现get请求 并返回结果 path:请求资源路径
public  static String get(String path) {
    BufferedReader reader = null;
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    String line = "";//最开始空行
    String result = "";//累计结果
    try {
        //Union Resource Location
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        //进行链接
        conn.connect();

        inputStream = conn.getInputStream();
        //字符流：按行读写 只能处理文字 字符读取流 reader 字符写入流 writer 缓冲读取流 bufferedreader 缓冲写入流 bufferedwriter
        //字节流 最基础 能够处理所有数据 字节输入流 inputstream 字节输出流 outputstream 缓冲输入流 bufferedinputstrean 缓冲输出流bufferedoutstream
        //转换流 字节读取流 inputreader 字节写入流 outputwriter

        //字符流 利用转换流把字节流转换成字符流
        reader = new BufferedReader(new InputStreamReader(inputStream));


        while ((line = reader.readLine()) != null) {
            result = result + line;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭读取流
// 按开的顺序 反向关闭 例如 1》2》3》 关闭则 3》2》1;不能更换顺序
        try {
            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader !=null){
                inputStreamReader.close();
            }
            if (inputStream !=null){
                inputStream.close();
            }

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
    return result;
}

//post封装
public  static  String post(String path, Map<String,String> header, byte[]data){
    OutputStream outputStream =null;
    BufferedReader reader=null;
    String result="";

    try {
        URL url = new URL(path);
        HttpURLConnection conn =(HttpURLConnection) url.openConnection();
        // url.openConnection();


        conn.setRequestMethod("POST");
        //设置可以亲求发送数据
        conn.setDoInput(true);
        conn.setDoOutput(true);



        if (!header.isEmpty()){
            //header 不为空 将header中的数据遍历,设置到请求头中
            Set<String> keys = header.keySet();
            for (String key :keys){
                String value = header.get(key);
                conn.setRequestProperty(key,value);
            }

        }

        conn.connect();
        //把请求头数据发送到请求服务端
        outputStream =conn.getOutputStream();
        outputStream.write(data);
        outputStream.flush();

        //等待接收返回数据

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line =null;
        while ((line =reader.readLine())!=null){
            result += line;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader !=null){
                reader.close();
            }
            if (outputStream !=null){
                outputStream.close();
            }

        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}
}
